﻿using System;
using DMS.BaseClass;
using DMS.Business.Modules.ArchivesManagement;
using DMS.Business.Modules.Organization;
using DMS.Data;
using DMS.Business.Utility;

namespace DMS.Web.Modules.ArchivesManagement
{
    public partial class BaseInfoForm : System.Web.UI.Page
    {
        /// <summary>
        /// 表单ID
        /// </summary>
        private string _id = "";
        /// <summary>
        /// 操作类型，包括New/Edit/View
        /// </summary>
        private ActionType _action;
        /// <summary>
        /// 基本信息类型，包括DW/GTH/GR
        /// </summary>
        private BaseInfoType _baseInfoType;
        
        public string DefaultDisplayStyle
        {
            get { return Request["baseInfoType"] == "GR" ? "none" : ""; }
        }

        public string GRDisplayStyle
        {
            get { return Request["baseInfoType"] != "GR" ? "none" : ""; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            _id = Request["id"];
            _action = (ActionType)Enum.Parse(typeof(ActionType), Request["action"]);
            _baseInfoType = (BaseInfoType)Enum.Parse(typeof(BaseInfoType), Request["baseInfoType"]);
            this.ArchInfoList.BaseInfoId = _id;
            if (!IsPostBack)
            {
                InitControls();
                InitData();
            }
        }

        /// <summary>
        /// 初始化页面控件
        /// </summary>
        private void InitControls()
        {
            InitPageTitle();
            LoadArchInfoList();
            
            if(_baseInfoType==BaseInfoType.GR)
            {
                PayerNameValidator.Enabled = true;
                IdentityCardNoValidator.Enabled = true;

                UnitNameValidator.Enabled = false;

                this.InsuranceCount.Text = "1";
            }

            if (!SystemContext.Current.LoginUser.IsInRole("档案管理员;系统管理员;"))
            {
                this.btnSave.Visible = false;
            }
        }

        #region 初始化页面元素
        /// <summary>
        /// 页面标题
        /// </summary>
        public string PageTitle = "基本资料信息";

        /// <summary>
        /// 根据类型初始化页面标题
        /// </summary>
        protected  void InitPageTitle()
        {
            switch (_baseInfoType)
            {
                case BaseInfoType.DW:
                    PageTitle = "单位基本信息";
                    break;
                case BaseInfoType.GTH:
                    PageTitle = "个体户基本信息";
                    break;
                case BaseInfoType.GR:
                    PageTitle = "个人基本信息";
                    break;
            }
        }

        /// <summary>
        /// 加载当前实体对应的资料信息列表
        /// </summary>
        protected void LoadArchInfoList()
        {
            
        }
        #endregion

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            if (_id != "")
            {
                AM_BaseInfo baseInfo = BaseInfoService.GetBaseInfoById(_id);
                FormBinder.BindObjectToControls(baseInfo, divContent);
                if(baseInfo.RegistDate!=null)
                {
                    this.RegistDate.Text = ((DateTime) baseInfo.RegistDate).ToString("yyyy-MM-dd");
                }

                if(baseInfo.DeptID==null)
                {
                    Department _currentBigDept = SystemContext.Current.LoginUser.BigDept;
                    this.DeptName.Value = _currentBigDept.Name; 
                    this.DeptID.Value = _currentBigDept.ID; 
                }
            }else
            {
                this.BaseInfoID.Value = Guid.NewGuid().ToString();
                this.Type.Value = _baseInfoType.ToString();
                Department _currentBigDept = SystemContext.Current.LoginUser.BigDept;
                this.DeptName.Value = _currentBigDept.Name; 
                this.DeptID.Value = _currentBigDept.ID; 
            }
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
            {
                return;
            }

            try
            {
                AM_BaseInfo baseInfo;
                if (_action == ActionType.New)
                {
                    baseInfo = new AM_BaseInfo();
                }
                else
                {
                    baseInfo = BaseInfoService.GetBaseInfoById(_id);
                }
                FormBinder.BindControlsToObject(baseInfo, divContent);
                
                BaseInfoService.SaveOrUpdateBaseInfo(baseInfo);

                ClientHelper.Instance.CloseWindow("保存成功", "ok", RefreshType.OpenWindow);
            }
            catch (Exception ex)
            {
                string code = LogHelper.LogException(ex);
                string msg = string.Format("操作失败，请联系管理员。\\r\\n错误码：{0}", code);
                ClientHelper.Instance.Alert(msg);
            }
        }


    }
}